#include <stdio.h>
#include <stdlib.h>
#include "coro.h"
#include <string.h>

char *global_dir;
char *global_name;
coro_t *finder;
coro_t *fs_walker;

void usage()
{
    puts("find -name file-name dir");
}

extern void fs_walk();
void find()
{
	while(1) {
		char *item = (char *) coro_resume(fs_walker);
		if (item == NULL)
			break;
        //printf("%s\n", item);

		const char ch = '/';
		char *p = strrchr(item, ch);
        if (p == NULL) continue;
        //printf("%s\n", p + 1);
		if (strcmp(p+1, global_name) == 0) {
			puts(item);
		}	
        
	}
	exit(0);
}

int main(int argc, char *argv[])
{

    	if (argc != 4 || strcmp(argv[1], "-name") != 0) {
		usage();
		return 0;
    	}
	global_dir = argv[2];
	global_name = argv[3];
	puts("begin\n");
    //printf("%s\n", global_dir);
    //printf("%s\n", global_name);
	finder = coro_new(find);
	fs_walker = coro_new(fs_walk);
	coro_boot(finder);
	coro_delete(finder);
	coro_delete(fs_walker);
    return 0;
}
